package tfmcbs.diagram.edit.policies;

import java.util.Iterator;

import org.eclipse.emf.ecore.EAnnotation;
import org.eclipse.gef.commands.Command;
import org.eclipse.gmf.runtime.diagram.core.commands.DeleteCommand;
import org.eclipse.gmf.runtime.emf.commands.core.command.CompositeTransactionalCommand;
import org.eclipse.gmf.runtime.emf.type.core.commands.DestroyElementCommand;
import org.eclipse.gmf.runtime.emf.type.core.commands.DestroyReferenceCommand;
import org.eclipse.gmf.runtime.emf.type.core.requests.CreateRelationshipRequest;
import org.eclipse.gmf.runtime.emf.type.core.requests.DestroyElementRequest;
import org.eclipse.gmf.runtime.emf.type.core.requests.DestroyReferenceRequest;
import org.eclipse.gmf.runtime.emf.type.core.requests.ReorientReferenceRelationshipRequest;
import org.eclipse.gmf.runtime.emf.type.core.requests.ReorientRelationshipRequest;
import org.eclipse.gmf.runtime.notation.Edge;
import org.eclipse.gmf.runtime.notation.View;

import tfmcbs.diagram.edit.commands.ConnectionCreateCommand;
import tfmcbs.diagram.edit.commands.ConnectionReorientCommand;
import tfmcbs.diagram.edit.commands.DependencyCreateCommand;
import tfmcbs.diagram.edit.commands.DependencyReorientCommand;
import tfmcbs.diagram.edit.commands.EntityExtendsCreateCommand;
import tfmcbs.diagram.edit.commands.EntityExtendsReorientCommand;
import tfmcbs.diagram.edit.commands.ManifestationCreateCommand;
import tfmcbs.diagram.edit.commands.ManifestationReorientCommand;
import tfmcbs.diagram.edit.commands.PrimitiveRealizationCreateCommand;
import tfmcbs.diagram.edit.commands.PrimitiveRealizationReorientCommand;
import tfmcbs.diagram.edit.commands.PrimitiveUsageCreateCommand;
import tfmcbs.diagram.edit.commands.PrimitiveUsageReorientCommand;
import tfmcbs.diagram.edit.parts.ConnectionEditPart;
import tfmcbs.diagram.edit.parts.DependencyEditPart;
import tfmcbs.diagram.edit.parts.EntityExtendsEditPart;
import tfmcbs.diagram.edit.parts.ManifestationEditPart;
import tfmcbs.diagram.edit.parts.PrimitiveRealizationEditPart;
import tfmcbs.diagram.edit.parts.PrimitiveUsageEditPart;
import tfmcbs.diagram.part.ComponentBasedSystemVisualIDRegistry;
import tfmcbs.diagram.providers.ComponentBasedSystemElementTypes;

/**
 * @generated
 */
public class PrimitiveComponentItemSemanticEditPolicy extends
		ComponentBasedSystemBaseItemSemanticEditPolicy {

	/**
	 * @generated
	 */
	public PrimitiveComponentItemSemanticEditPolicy() {
		super(ComponentBasedSystemElementTypes.PrimitiveComponent_2022);
	}

	/**
	 * @generated
	 */
	protected Command getDestroyElementCommand(DestroyElementRequest req) {
		View view = (View) getHost().getModel();
		CompositeTransactionalCommand cmd = new CompositeTransactionalCommand(
				getEditingDomain(), null);
		cmd.setTransactionNestingEnabled(false);
		for (Iterator<?> it = view.getTargetEdges().iterator(); it.hasNext();) {
			Edge incomingLink = (Edge) it.next();
			if (ComponentBasedSystemVisualIDRegistry.getVisualID(incomingLink) == ConnectionEditPart.VISUAL_ID) {
				DestroyElementRequest r = new DestroyElementRequest(
						incomingLink.getElement(), false);
				cmd.add(new DestroyElementCommand(r));
				cmd.add(new DeleteCommand(getEditingDomain(), incomingLink));
				continue;
			}
			if (ComponentBasedSystemVisualIDRegistry.getVisualID(incomingLink) == DependencyEditPart.VISUAL_ID) {
				DestroyElementRequest r = new DestroyElementRequest(
						incomingLink.getElement(), false);
				cmd.add(new DestroyElementCommand(r));
				cmd.add(new DeleteCommand(getEditingDomain(), incomingLink));
				continue;
			}
			if (ComponentBasedSystemVisualIDRegistry.getVisualID(incomingLink) == ManifestationEditPart.VISUAL_ID) {
				DestroyElementRequest r = new DestroyElementRequest(
						incomingLink.getElement(), false);
				cmd.add(new DestroyElementCommand(r));
				cmd.add(new DeleteCommand(getEditingDomain(), incomingLink));
				continue;
			}
			if (ComponentBasedSystemVisualIDRegistry.getVisualID(incomingLink) == EntityExtendsEditPart.VISUAL_ID) {
				DestroyReferenceRequest r = new DestroyReferenceRequest(
						incomingLink.getSource().getElement(), null,
						incomingLink.getTarget().getElement(), false);
				cmd.add(new DestroyReferenceCommand(r));
				cmd.add(new DeleteCommand(getEditingDomain(), incomingLink));
				continue;
			}
		}
		for (Iterator<?> it = view.getSourceEdges().iterator(); it.hasNext();) {
			Edge outgoingLink = (Edge) it.next();
			if (ComponentBasedSystemVisualIDRegistry.getVisualID(outgoingLink) == ConnectionEditPart.VISUAL_ID) {
				DestroyElementRequest r = new DestroyElementRequest(
						outgoingLink.getElement(), false);
				cmd.add(new DestroyElementCommand(r));
				cmd.add(new DeleteCommand(getEditingDomain(), outgoingLink));
				continue;
			}
			if (ComponentBasedSystemVisualIDRegistry.getVisualID(outgoingLink) == DependencyEditPart.VISUAL_ID) {
				DestroyElementRequest r = new DestroyElementRequest(
						outgoingLink.getElement(), false);
				cmd.add(new DestroyElementCommand(r));
				cmd.add(new DeleteCommand(getEditingDomain(), outgoingLink));
				continue;
			}
			if (ComponentBasedSystemVisualIDRegistry.getVisualID(outgoingLink) == PrimitiveUsageEditPart.VISUAL_ID) {
				DestroyElementRequest r = new DestroyElementRequest(
						outgoingLink.getElement(), false);
				cmd.add(new DestroyElementCommand(r));
				cmd.add(new DeleteCommand(getEditingDomain(), outgoingLink));
				continue;
			}
			if (ComponentBasedSystemVisualIDRegistry.getVisualID(outgoingLink) == PrimitiveRealizationEditPart.VISUAL_ID) {
				DestroyElementRequest r = new DestroyElementRequest(
						outgoingLink.getElement(), false);
				cmd.add(new DestroyElementCommand(r));
				cmd.add(new DeleteCommand(getEditingDomain(), outgoingLink));
				continue;
			}
			if (ComponentBasedSystemVisualIDRegistry.getVisualID(outgoingLink) == EntityExtendsEditPart.VISUAL_ID) {
				DestroyReferenceRequest r = new DestroyReferenceRequest(
						outgoingLink.getSource().getElement(), null,
						outgoingLink.getTarget().getElement(), false);
				cmd.add(new DestroyReferenceCommand(r));
				cmd.add(new DeleteCommand(getEditingDomain(), outgoingLink));
				continue;
			}
		}
		EAnnotation annotation = view.getEAnnotation("Shortcut"); //$NON-NLS-1$
		if (annotation == null) {
			// there are indirectly referenced children, need extra commands: false
			addDestroyShortcutsCommand(cmd, view);
			// delete host element
			cmd.add(new DestroyElementCommand(req));
		} else {
			cmd.add(new DeleteCommand(getEditingDomain(), view));
		}
		return getGEFWrapper(cmd.reduce());
	}

	/**
	 * @generated
	 */
	protected Command getCreateRelationshipCommand(CreateRelationshipRequest req) {
		Command command = req.getTarget() == null ? getStartCreateRelationshipCommand(req)
				: getCompleteCreateRelationshipCommand(req);
		return command != null ? command : super
				.getCreateRelationshipCommand(req);
	}

	/**
	 * @generated
	 */
	protected Command getStartCreateRelationshipCommand(
			CreateRelationshipRequest req) {
		if (ComponentBasedSystemElementTypes.Connection_4037 == req
				.getElementType()) {
			return getGEFWrapper(new ConnectionCreateCommand(req,
					req.getSource(), req.getTarget()));
		}
		if (ComponentBasedSystemElementTypes.Dependency_4038 == req
				.getElementType()) {
			return getGEFWrapper(new DependencyCreateCommand(req,
					req.getSource(), req.getTarget()));
		}
		if (ComponentBasedSystemElementTypes.Manifestation_4039 == req
				.getElementType()) {
			return null;
		}
		if (ComponentBasedSystemElementTypes.PrimitiveUsage_4050 == req
				.getElementType()) {
			return getGEFWrapper(new PrimitiveUsageCreateCommand(req,
					req.getSource(), req.getTarget()));
		}
		if (ComponentBasedSystemElementTypes.PrimitiveRealization_4042 == req
				.getElementType()) {
			return getGEFWrapper(new PrimitiveRealizationCreateCommand(req,
					req.getSource(), req.getTarget()));
		}
		if (ComponentBasedSystemElementTypes.EntityExtends_4046 == req
				.getElementType()) {
			return getGEFWrapper(new EntityExtendsCreateCommand(req,
					req.getSource(), req.getTarget()));
		}
		return null;
	}

	/**
	 * @generated
	 */
	protected Command getCompleteCreateRelationshipCommand(
			CreateRelationshipRequest req) {
		if (ComponentBasedSystemElementTypes.Connection_4037 == req
				.getElementType()) {
			return getGEFWrapper(new ConnectionCreateCommand(req,
					req.getSource(), req.getTarget()));
		}
		if (ComponentBasedSystemElementTypes.Dependency_4038 == req
				.getElementType()) {
			return getGEFWrapper(new DependencyCreateCommand(req,
					req.getSource(), req.getTarget()));
		}
		if (ComponentBasedSystemElementTypes.Manifestation_4039 == req
				.getElementType()) {
			return getGEFWrapper(new ManifestationCreateCommand(req,
					req.getSource(), req.getTarget()));
		}
		if (ComponentBasedSystemElementTypes.PrimitiveUsage_4050 == req
				.getElementType()) {
			return null;
		}
		if (ComponentBasedSystemElementTypes.PrimitiveRealization_4042 == req
				.getElementType()) {
			return null;
		}
		if (ComponentBasedSystemElementTypes.EntityExtends_4046 == req
				.getElementType()) {
			return getGEFWrapper(new EntityExtendsCreateCommand(req,
					req.getSource(), req.getTarget()));
		}
		return null;
	}

	/**
	 * Returns command to reorient EClass based link. New link target or source
	 * should be the domain model element associated with this node.
	 * 
	 * @generated
	 */
	protected Command getReorientRelationshipCommand(
			ReorientRelationshipRequest req) {
		switch (getVisualID(req)) {
		case ConnectionEditPart.VISUAL_ID:
			return getGEFWrapper(new ConnectionReorientCommand(req));
		case DependencyEditPart.VISUAL_ID:
			return getGEFWrapper(new DependencyReorientCommand(req));
		case ManifestationEditPart.VISUAL_ID:
			return getGEFWrapper(new ManifestationReorientCommand(req));
		case PrimitiveUsageEditPart.VISUAL_ID:
			return getGEFWrapper(new PrimitiveUsageReorientCommand(req));
		case PrimitiveRealizationEditPart.VISUAL_ID:
			return getGEFWrapper(new PrimitiveRealizationReorientCommand(req));
		}
		return super.getReorientRelationshipCommand(req);
	}

	/**
	 * Returns command to reorient EReference based link. New link target or source
	 * should be the domain model element associated with this node.
	 * 
	 * @generated
	 */
	protected Command getReorientReferenceRelationshipCommand(
			ReorientReferenceRelationshipRequest req) {
		switch (getVisualID(req)) {
		case EntityExtendsEditPart.VISUAL_ID:
			return getGEFWrapper(new EntityExtendsReorientCommand(req));
		}
		return super.getReorientReferenceRelationshipCommand(req);
	}

}
